package club.emergency.wechat.authorization.search.provider.impl;

import club.emergency.wechat.authorization.search.AuthorizationSearch;
import club.emergency.wechat.common.response.ResponseEntity;
import club.emergency.wechat.common.response.StatusCode;
import club.emergency.wechat.model.authorization.Authorization;
import club.emergency.wechat.model.role.Role;
import club.emergency.wechat.persistence.authorization.AuthorizationHasRoleMapper;
import club.emergency.wechat.persistence.authorization.AuthorizationMapper;
import club.emergency.wechat.persistence.role.RoleMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * Create with IntelliJ IDEA
 * Project Name: wechat
 * Package Name: club.emergency.wechat.authorization.search.provider.impl
 * Date: 2019/3/14
 *
 * @author: 快乐的小菌菌
 * Description:
 */
@RestController
public class AuthorizationSearchImpl implements AuthorizationSearch {

    private RoleMapper roleMapper;

    private AuthorizationMapper authorizationMapper;

    private AuthorizationHasRoleMapper authorizationHasRoleMapper;

    public AuthorizationSearchImpl(RoleMapper roleMapper, AuthorizationMapper authorizationMapper, AuthorizationHasRoleMapper authorizationHasRoleMapper) {
        this.roleMapper = roleMapper;
        this.authorizationMapper = authorizationMapper;
        this.authorizationHasRoleMapper = authorizationHasRoleMapper;
    }

    @Override
    public String flipListForRole(@RequestParam(value = "roleName", required = false) String roleName, @RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize) {
        Page<Role> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(Objects.nonNull(roleName), "role_name", roleName);
        queryWrapper.orderByAsc("id");
        IPage<Role> rollIPage = roleMapper.selectPage(page, queryWrapper);
        return ResponseEntity.ok(StatusCode.DB_SEARCH_OK, "查询角色数据成功！", rollIPage);
    }

    @Override
    public String flipListAuthorizationByRoleId(@RequestParam("roleId") Long roleId) {
        List<Long> authorizationIdList = authorizationHasRoleMapper.selectAuthorizationIdListByRoleId(roleId);
        List<Authorization> authorizations = null;
        if (authorizationIdList.size() != 0) {
            authorizations = authorizationMapper.selectBatchIds(authorizationIdList);
        }
        return ResponseEntity.ok(StatusCode.DB_SEARCH_OK, "查询角色对应的权限列表成功！", authorizations);
    }

    @Override
    public String flipListAuthorizationByCode(@RequestParam(value = "code", required = false) Long code, @RequestParam(value = "parentId", required = false) Long parentId) {
        QueryWrapper<Authorization> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if (Objects.isNull(code) && Objects.isNull(parentId)) {
            queryWrapper.likeRight("code", "1");
            queryWrapper.isNull("parent_id");
        } else {
            queryWrapper.likeRight("code", code);
            queryWrapper.eq("parent_id", parentId);
        }
        List<Authorization> authorizations = authorizationMapper.selectList(queryWrapper);
        return ResponseEntity.ok(StatusCode.DB_SEARCH_OK, "根据code查询权限列表成功！", authorizations);
    }
}
